import api from "../../utils/api";

// pages/invite/invite.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    progress: 2 as number,
    type: 1 as number,//1-审核中，2-审核未通过
    name: "" as string,
    phone: "" as string,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const status = options.status
    this.setData({
      progress: status == 'NONE' ? 1 : 2,
      type: status == "APPLIED" ? 1 : 2
    })
  },
  back(): void {
    wx.navigateBack();
  },
  // 重新申请
  toApply(): void {
    this.setData({
      progress: 1
    })
  },
  // 处理输入
  handleName(e: any): void {
    this.setData({
      name: e.detail.value
    })
  },
  handlePhone(e: any): void {
    this.setData({
      phone: e.detail.value
    })
  },
  // 提交
  submit(): void {
    const { name, phone } = this.data
    let mobilePattern = /^1[3-9]\d{9}$/;
    if (mobilePattern.test(phone)) {
      if (name) {
        api.reseller.applyForReseller({ name, phone }).then(res => {
          if (res.data.code == "000") {
            this.setData({
              type: 1,
              progress: 2
            }, () => {
              this.onLoad();
              this.onShow();
            })
          } else {
            wx.showToast({
              title: res.data.message as string,
              icon: "error"
            })
          }
        })
      } else {
        wx.showToast({
          title: "请输入姓名",
          icon: "error"
        })
      }
    } else {
      wx.showToast({
        title: "手机号格式错误",
        icon: "error"
      })
    }
  },
})